package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author Xiao
 * @date 2021/7/18
 */
public class TeacherController {
    private Scanner sc = new Scanner(System.in);
    private TeacherService ts = new TeacherService();
    public void entry() {
        while (true) {
            System.out.println("--------欢迎使用黑马<老师>管理系统--------");
            System.out.println("1.添加老师\t2.删除老师\t3.修改老师\t4.查询老师\t5.退出");
            System.out.println("请输入您的选择：");
            String choice = sc.next();
            switch (choice) {
                case "1":
//                    System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
//                    System.out.println("删除老师");
                    deleteTeacher();
                    break;
                case "3":
//                    System.out.println("修改老师");
                    updateTeacher();
                    break;
                case "4":
//                    System.out.println("查询老师");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢您的使用。");
                    return;
                default:
                    System.out.println("您输入的选项有误。");
                    break;
            }
        }
    }

    public final void updateTeacher() {
        String tid = inputTid();
        if (tid == null) {
            System.out.println("当前暂无老师信息，请添加后重试。");
            return;
        }
        Teacher t = inputTeacherInfo(tid);
        ts.updateTeacher(t,tid);
        System.out.println("修改成功！");
    }

    //删除老师
    public final void deleteTeacher() {
        String tid = inputTid();
        if (tid == null) {
            System.out.println("当前暂无老师信息，请添加后重试。");
            return;
        }
        boolean result = ts.deleteTeacher(tid);
        if (result) {
            System.out.println("删除成功！");
            return;
        }
        System.out.println("删除失败。");
    }

    //获取想要删除或修改的老师的工号
    public final String inputTid() {
        ArrayList<Teacher> list = ts.findAllTeacher();
        if (list == null) {
            return null;
        }
        while (true) {
            System.out.println("请输入老师工号");
            String tid = sc.next();
            boolean flag = ts.isExists(tid);
            if (flag) {
                return tid;
            }
            System.out.println("您输入的工号不存在。");
        }
    }

    //查找所有老师并打印
    public final void findAllTeacher() {
        ArrayList<Teacher> list = ts.findAllTeacher();
        if (list == null) {
            System.out.println("当前暂无老师信息，请添加后查询。");
            return;
        }
        System.out.println("工号\t\t姓名\t年龄\t生日");
        for (Teacher t : list) {
            System.out.println(t.getId() + "\t" + t.getName() + "\t" + t.getAge() + "\t\t" + t.getBirthday());
        }
    }

    //添加老师到集合
    public final void addTeacher() {
        Teacher t = new Teacher();
        String tid;
        while (true) {
            System.out.println("请输入工号：");
            tid = sc.next();
            boolean flag = ts.isExists(tid);
            if (!flag) {
                break;
            }
            System.out.println("您输入的工号已存在。");
        }
        t = inputTeacherInfo(tid);
        boolean result = ts.addTeacher(t);
        if (result) {
            System.out.println("添加成功！");
            return;
        }
        System.out.println("添加失败。");

    }

    //键入老师姓名、年龄、生日的信息，封装为老师对象并返回
    public final Teacher inputTeacherInfo(String tid) {
        Teacher t = new Teacher();
        t.setId(tid);
        System.out.println("请输入老师姓名：");
        t.setName(sc.next());
        System.out.println("请输入老师年龄：");
        t.setAge(sc.nextInt());
        System.out.println("请输入老师生日：");
        t.setBirthday(sc.next());
        return t;
    }
}
